/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.pb.sistcomp.dao;

import br.pb.sistcomp.entity.Lance;
import br.pb.sistcomp.entity.Pessoa;
import br.pb.sistcomp.exception.*;
import br.pb.sistcomp.util.HibernateUtil;
import java.util.List;
import javax.persistence.EntityManager;

/**
 * Class that represents an entity in the system
 *
 * @author George Alcantara, george.lima@dce.ufpb.br
 * @version 1.0
 *
 * Copyright (C) 2011 SistComp.
 */

public class LanceDaoImpl implements LanceDao{
    private EntityManager em;

    /**
     * Default constructor of the class
     */

    public LanceDaoImpl(){
        em = HibernateUtil.getInstance().getFactory().createEntityManager();
    }

    /**
     * Method in order to keep a bid in the database.
     * @param lance Lance object type that contains all information of the bid to be registered.
     */

    @Override
    public void salvarLance(Lance lance) throws ErroAoSalvarException{
        try{
            em.getTransaction().begin();
            em.persist(lance);
            em.getTransaction().commit();
        }catch(Exception e){
            em.getTransaction().rollback();
            throw new ErroAoSalvarException(e);
        }
    }

    /**
     * Method in order to remove a bid in the database.
     * @param lance Lance object type that holds all the information of the bid to be removed.
     */

    @Override
    public void removerLance(Lance lance) throws ErroAoRemoverException{
        try{
            em.getTransaction().begin();
            em.remove(lance);
            em.getTransaction().commit();
        }catch(Exception e){
            em.getTransaction().rollback();
            throw new ErroAoRemoverException(e);
        }
            
    }

    /*
     * Metodo temporario de edicao, que talvez nao seja preciso.
     * 
    @Override
    public void editarLance(Lance lance){
            System.out.println(lance.toString());
            em.getTransaction().begin();
            lance = em.merge(lance);
            em.getTransaction().commit();
    }
    */
    

    /**
     * Method with the purpose of listing all the moves in the database.
     * @return Returns a list of all the bids registered on the database.
     */
    
    @Override
    public List<Lance> listarTodos() {
        return em.createQuery("SELECT l FROM Lance l").getResultList();
    }

    /**
     * Method as purpose of conducting the search for a bid in the database.
     * @param id of type int that represents the ID of the bid to be searched in the database.
     * @return Returns the bid sought in the database.
     */

    @Override
    public Lance buscarLance(Integer id) {
        return em.find(Lance.class,id);
    }

    /**
     * Method with the purpose of seeking a Lance apartir the person who gave the bid.
     * @param Person type person representing the person who gave Lance the database.
     * @return Returns the bid given by the desired person.
     */

    @Override
    public List<Lance> buscarLancesPorUsuario(Pessoa pessoa) {
        return em.createQuery("SELECT l FROM Lance l WHERE l.pessoa = :pessoa").setParameter(":pessoa", pessoa).getResultList();
    }
    
}
